package com.wswy.wzcx.module.upgrade;

import android.content.Context;
import android.util.Log;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.BuglyStrategy;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.interfaces.BetaPatchListener;
import com.wswy.wzcx.BuildConfig;
import com.wswy.wzcx.R;
import com.wswy.wzcx.module.ChannelManager;
import com.wswy.wzcx.module.GlobalConfigManager;
import com.wswy.wzcx.ui.activity.MineActivity;
import com.wswy.wzcx.ui.activity.ModuleActivity;
import com.wswy.wzcx.ui.main.MainActivity;

public class UpgradeConfigure {

  private static final String TAG = "UpgradeConfigure";

  public static void init(Context context){
    Beta.largeIconId = R.drawable.umeng_push_notification_default_large_icon;
    Beta.smallIconId = R.drawable.umeng_push_notification_default_small_icon;
    Beta.upgradeDialogLayoutId = R.layout.upgrade_dialog;
    Beta.enableHotfix = true;
    Beta.canAutoDownloadPatch = true;
    Beta.canAutoPatch = true;
    Beta.canNotifyUserRestart = false;

    Beta.betaPatchListener = getPatchListener();

    Beta.autoCheckUpgrade = false;
    Beta.canShowApkInfo = false;
    Beta.autoDownloadOnWifi = true;

    Beta.canShowUpgradeActs.add(GlobalConfigManager.getInstance().getFlavorConfigure().getMainActClass());
//    Beta.canShowUpgradeActs.add(MainActivity.class);
    Beta.canShowUpgradeActs.add(MineActivity.class);
    Beta.canShowUpgradeActs.add(ModuleActivity.class);

    BuglyStrategy strategy = new BuglyStrategy();
    strategy.setEnableANRCrashMonitor(false);
    strategy.setEnableNativeCrashMonitor(false);
    strategy.setAppChannel(ChannelManager.getChannel());

    if (BuildConfig.DEBUG){
      Bugly.setIsDevelopmentDevice(context,true);
    }

    Bugly.init(context, BuildConfig.BUGLY_APPID, BuildConfig.DEBUG,strategy);
  }

  public static void checkUpgrade(boolean isManual, boolean isSilence){
    Beta.checkUpgrade(isManual, isSilence);
  }

  private static BetaPatchListener getPatchListener(){
    return new BetaPatchListener() {
      @Override
      public void onPatchReceived(String patchFileUrl) {
        Log.e(TAG, "onPatchReceived --> "+patchFileUrl);
      }

      @Override
      public void onDownloadReceived(long savedLength, long totalLength) {
        Log.e(TAG, "onDownloadReceived --> "+savedLength+"   "+totalLength);
      }

      @Override
      public void onDownloadSuccess(String patchFilePath) {
        Log.e(TAG, "onDownloadSuccess --> "+patchFilePath);
      }

      @Override
      public void onDownloadFailure(String msg) {
        Log.e(TAG, "onDownloadFailure --> "+msg);
      }

      @Override
      public void onApplySuccess(String msg) {
        Log.e(TAG, "onApplySuccess --> "+msg);
      }

      @Override
      public void onApplyFailure(String msg) {
        Log.e(TAG, "onApplyFailure --> "+msg);
      }

      @Override
      public void onPatchRollback() {
        Log.e(TAG, "onPatchRollback --> ");
      }
    };
  }


  public static void installTinker(Object applicationLike){
    try {
      Beta.installTinker(applicationLike);
    }catch (Throwable e){
      e.printStackTrace();
    }
  }

}
